package com.example.demo.server.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.example.demo.entity.base.PageRequestBean;
import com.example.demo.entity.base.PageResponseBean;
import com.example.demo.entity.base.ResponseEntity;
import com.example.demo.entity.server.ServerListenEntity;
import com.example.demo.entity.server.ServerReportEntity;
import com.example.demo.server.dao.ServerListenDao;
import com.example.demo.util.CommonUtil;
import com.example.demo.util.PrimaryKeyUtil;
import com.example.demo.util.ResponseFactory;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
@Transactional
public class ServerListenService {
	
	@Autowired
	private ServerListenDao serverListenDao;
	
	/**
	 * @author 许子文
	 * @Title: viewServerReport
	 * @Description: 查看服务报告
	 * @return
	 */
	public ResponseEntity<ServerReportEntity> viewServerReport(String server_id) {
		
		List<ServerReportEntity> serverReports = null;
		
		try {
			serverReports = serverListenDao.viewServerReport(server_id);
		} catch (Exception e) {
			log.error("查看服务报告失败",e);
			return ResponseFactory.setFail("查看服务报告失败");
		}
		
		return ResponseFactory.setList(serverReports);
	}
	/**
	 * @author 许子文
	 * @Title: queryListenedServer
	 * @Description: 查询被监听的服务
	 * @return
	 */
	public ResponseEntity<PageResponseBean<ServerListenEntity>> queryListenedServer(PageRequestBean<ServerListenEntity> pagerequest) {
		
		int pageNo = pagerequest.getPageNum();
		int pageSize = pagerequest.getPageSize();
		
		ServerListenEntity serverParam = pagerequest.getParams();
		
		if(null==serverParam) {
			serverParam = new ServerListenEntity();
		}
		
		Page<ServerListenEntity> page = PageHelper.startPage(pageNo, pageSize, true);
		
		List<ServerListenEntity> servers = null;
		
		try {
			servers = serverListenDao.queryListenedServer(serverParam);
		} catch (Exception e) {
			log.error("查询服务状态失败",e);
			return ResponseFactory.setFail("查询服务状态失败");
		}
		
		long total = page.getTotal();
		
		int totalPages;
		
		try {
			totalPages = CommonUtil.getTotalPages(pageSize, total);
		} catch (Exception e) {
			log.error("获取总页数时失败",e);
			return ResponseFactory.setFail("获取总页数时失败");
		}
		
		
		PageResponseBean<ServerListenEntity> pageResp = new PageResponseBean<>(total, pageSize, pageNo, totalPages, servers);
		
		return ResponseFactory.setObject(pageResp);
	}
	
	/**
	 * @author 许子文
	 * @Title: deleteServer
	 * @Description: 删除服务
	 * @param key_id
	 * @return
	 */
	public ResponseEntity<String> deleteServer(String key_id) {
		
		try {
			serverListenDao.deleteServer(key_id);
		} catch (Exception e) {
			log.error("删除服务失败",e);
			return ResponseFactory.setFail("删除服务失败");
		}
		
		return ResponseFactory.setSuccess("删除服务成功!");
	}
	
	/**
	 * @author 许子文
	 * @Title: updateServerParam
	 * @Description: 修改服务属性
	 * @param serverListenEntity
	 * @return
	 */
	public ResponseEntity<String> updateServerParam(ServerListenEntity serverListenEntity) {
		
		try {
			serverListenDao.updateServerParam(serverListenEntity);
		} catch (Exception e) {
			log.error("修改服务属性失败",e);
			return ResponseFactory.setFail("修改服务属性失败");
		}
		
		return ResponseFactory.setSuccess("修改服务属性成功");
	}
	
	/**
	 * @author 许子文
	 * @Title: addListenServer
	 * @Description: 添加监听服务
	 * @param serverListenEntity
	 * @return
	 */
	public ResponseEntity<String> addListenServer(ServerListenEntity serverListenEntity) {
		
		serverListenEntity.setKey_id(PrimaryKeyUtil.getUUIDWithOutLink());
		
		try {
			serverListenDao.addListenServer(serverListenEntity);
		} catch (Exception e) {
			log.error("添加监听服务失败",e);
			return ResponseFactory.setFail("添加监听服务失败");
		}
		
		
		return ResponseFactory.setSuccess("添加监听服务成功");
	}
}
